package cn.kent.util;

import java.util.Stack;

/**
 * 二进制转换工具类
 */
public class BinaryUtil {
    public static void main(String[] args) {
        int num = 8;
        String s = intConvertToStr(num);
        System.out.println("十进制 -> 二进制：" + num + " -> " + s);
    }

    /**
     * 二进制转十进制
     */
    private static String intConvertToStr(Integer num) {
        StringBuilder s = new StringBuilder();
        Stack<Integer> q = new Stack<>();
        while (num != 0) {
            int i = num % 2;
            num /= 2;
            q.push(i);
        }
        while (!q.isEmpty()) {
            s.append(q.pop());
        }
        return s.toString();
    }
}
